package ej1.Main;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class InputParser {

	public InputParser(){}
	
	public Iterable<ProblemInstance> Parse(String aFilePath){
		
		ArrayList<ProblemInstance> instances = new ArrayList<ProblemInstance>();
		
		try {
			
			FileReader reader = new FileReader(aFilePath);
			BufferedReader bufferedReader = new BufferedReader(reader);
			
			String line = bufferedReader.readLine(); 
			
			while (line != null && !line.equals("#")){
			
				ProblemInstance instance = new ProblemInstance();
				
				String[] nodesAndEdges = line.split(" ");
				
				instance.setStrainCount(Integer.parseInt(nodesAndEdges[0]));
				instance.setMutationCount(Integer.parseInt(nodesAndEdges[1]));
				
				for (int i = 1; i <= instance.getStrainCount(); i++)
					instance.addStrain(i);
				
				for (int i = 0; i < instance.getMutationCount(); i++){
					
					line = bufferedReader.readLine();
					String[] mutation = line.split(" ");
					
					instance.AddMutation(Integer.parseInt(mutation[0]), Integer.parseInt(mutation[1]));
				}
				
				instances.add(instance);
				
				line = bufferedReader.readLine();
			}
			
			bufferedReader.close();
			reader.close();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return instances;
	}
	
	
	
}
